<?php
/**
 * @version		$Id: weblinks.php 8117 2007-07-20 13:37:22Z friesengeist $
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.model');

/**
 * Weblinks Component Weblink Model
 *
 * @author	Johan Janssens <johan.janssens@joomla.org>
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class BookstoreModelAnalytics extends JModel
{
	/**
	 * Category ata array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Category total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;

	/**
	 * Constructor
	 *
	 * @since 1.5
	 */
	function __construct()
	{
		parent::__construct();

		global $mainframe, $option;

		// Get the pagination request variables
		//TODO: use pagination and remove false limit
		$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', '', 'int' ); //$mainframe->getCfg('list_limit')
		$limitstart	= $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0, 'int' );
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to get weblinks item data
	 *
	 * @access public
	 * @return array
	 */
	 function getQuery(){
		 $query = $this->_buildQuery();
		 return $query;
	 }
	 
	function getData()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$id	= JRequest::getVar( 'id', '', 'get', 'int' );
			$query = $this->_buildQuery($id);
			if(!empty($id)){
				$this->_data = $this->_getList($query);//, $this->getState('limitstart'), $this->getState('limit'));
			}else{
				$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
			}//end if individual query
		}
		//var_dump($this->_data);
		return $this->_data;
	}

	/**
	 * Method to get the total number of weblink items
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}

	/**
	 * Method to get a pagination object for the weblinks
	 *
	 * @access public
	 * @return integer
	 */
	function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}

	function _buildQuery($id = '')
	{
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildContentWhere($id);
		$orderby	= $this->_buildContentOrderBy();
		if(empty($id)){
			//select date_format(a.querytime, '%M %e, %Y') as date, count(*) as searches from jos_bookstorequerylog as a group by date
			$query = "SELECT UNIX_TIMESTAMP(date(a.querytime)) as id, "
				. " date_format(a.querytime, '%M %e, %Y') as date,"
				. " (count(*) - ( 3 * 24) ) as searches" //3 autoload occur an hour
				. ' FROM #__bookstorequerylog AS a '
				. ' GROUP BY date'
				//. ' LEFT JOIN #__maps AS c ON c.id = a.map_id'
				//. ' LEFT JOIN #__users AS u ON u.id = a.checked_out '
				. $where
				. $orderby
			;
		}else{
			//TODO:  fix time offset; use joomla builtins
			$query = "SELECT UNIX_TIMESTAMP(date_format(a.querytime, '%Y-%m-%d %H:00:00')) as id, "
				. " hour(addtime(a.querytime, '-2:00:00.00')) as hour,"
				. " (count(*) - 3) as searches" //3 autoload occur an hour
				. ' FROM #__bookstorequerylog AS a '
				. " where UNIX_TIMESTAMP(addtime(a.querytime, '-2:00:00.00')) >= '"
				. $id
				."'"
				. " AND UNIX_TIMESTAMP(addtime(a.querytime, '-2:00:00.00')) <= '"
				.($id + 86400)
				."'"
				//. ' LEFT JOIN #__maps AS c ON c.id = a.map_id'
				//. ' LEFT JOIN #__users AS u ON u.id = a.checked_out '
				//. $where
				. ' GROUP BY hour'
				. $orderby
			;
		var_dump($query);
		}
		var_dump($query);
		return $query;
	}

	function _buildContentOrderBy()
	{
		global $mainframe, $option;

		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'a.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );

		if ($filter_order == 'a.ordering'){
			$orderby 	= ' ORDER BY a.querytime '.$filter_order_Dir;
		} else {
			$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir.' , a.querytime ';
		}
		return $orderby;
	}

	function _buildContentWhere($id = '')
	{
		global $mainframe, $option;

		$filter_state		= $mainframe->getUserStateFromRequest( $option.'filter_state',		'filter_state',		'',				'word' );
		$filter_catid		= $mainframe->getUserStateFromRequest( $option.'filter_catid',		'filter_catid',		0,				'int' );
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'filter_order',		'filter_order',		'a.ordering',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.'search',			'search',			'',				'string' );
		$search				= JString::strtolower( $search );
		(empty($id))? $id = JRequest::getVar( 'id', '', 'get', 'int' ) : $id = $id;
		//var_dump($id.'model');
		$where = array();
		//var_dump($id);
		if ($id > 0) {
			$where[] = 'a.id = '.(int) $id;
		}
		if ($search) {
			$where[] = '( LOWER(a.dept_cat_desc) LIKE '.$this->_db->Quote('%'.$search.'%')
					.' || LOWER(a.alias) LIKE '.$this->_db->Quote('%'.$search.'%')
					.')';
		}
		/*
		if ($filter_catid > 0) {
			$where[] = 'a.catid = '.(int) $filter_catid;
		}
		if ($search) {
			$where[] = 'LOWER(a.title) LIKE '.$this->_db->Quote('%'.$search.'%');
		}
		if ( $filter_state ) {
			if ( $filter_state == 'P' ) {
				$where[] = 'a.published = 1';
			} else if ($filter_state == 'U' ) {
				$where[] = 'a.published = 0';
			}
		}
		*/

		$where 		= ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
		return $where;
	}
	

	
}
?>
